package com.byedu.util;

import com.byedu.model.enums.LibraryTypeEnum;
import com.byedu.util.yunpian.util.StringUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.Arrays;

/**
 * Created by Luo on 2017-06-29.
 */
public class CommonUtil {

    public static boolean isEmpty(String str){
        return str==null||"".equals(str);
    }

    public static Integer StringToInt(String str){
        try{
            return Integer.parseInt(str);
        }catch(Exception e){
            return 0;
        }
    }

    public static Integer StringToPageInt(String str){
        try{
            return Integer.parseInt(str);
        }catch(Exception e){
            return 1;
        }
    }

    public static Double stringToDouble(String str){
  
        try{

            return Double.parseDouble(str);
        }catch(Exception e){
            return 0.0;
        }
    }
    public static Integer nullInt(Integer str){

            if(str!=null){
            return str;
                }else{
                return 0;
            }

    }

    public static void outpostprint(HttpServletResponse res, String writeStr){
        try {
            res.setHeader("Content-type", "text/html;charset=UTF-8");
            //这句话的意思，是告诉servlet用UTF-8转码，而不是用默认的ISO8859
            res.setCharacterEncoding("UTF-8");
            PrintWriter out = res.getWriter();
            out.println(writeStr);
            System.out.print(writeStr);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getLibraryItemDesc (String libType, String libItemCateDesc, Integer libItemCateSeq) {
        String desc = "";
        if (LibraryTypeEnum.tip.toString().equals(libType)) {
            desc = "第" + libItemCateSeq + "章 " + libItemCateDesc;
        }

        if (LibraryTypeEnum.kaoqian.toString().equals(libType)) {
            desc = "模拟练习" + libItemCateSeq + " " + libItemCateDesc;
        }

        return desc;
    }



        public static String stringsort(String s) {
            if(!StringUtil.isNullOrEmpty(s)) {
                char[] arrayCh = s.toCharArray();
                Arrays.sort(arrayCh);
                String ss = Arrays.toString(arrayCh);
                return ss;
            }
            return s;
        }

    public static String doubletoStr(double ddd) {
        DecimalFormat df   = new DecimalFormat("######0.00");
        return df.format(ddd);
    }
}
